var express = require('express');
var router = express.Router();

var user = require('../models/user');
// var crypto = require('crypto');
// const init_token = 'TKL02o';
/* GET users listing. */
router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});
//用户登录
router.post('/login', function (req, res, next) {
    if (!req.body.username) {
        res.json({
            status: 1, //为了辨别密码对错和用户显示进行反馈-login-58
            message: "用户名为空"
        })
    }
    if (!req.body.password) {
        res.json({
            status: 1,
            message: "密码为空"
        })
    }
    user.findUserLogin(req.body.username, req.body.password, function (err, userSave) {
        if (userSave.length != 0) {
            res.json({
                status: 0,
                data: {
                    user: userSave
                },
                message: "用户登录成功"
            })
        } else {
            res.json({
                status: 1,
                message: "用户名或密码错误"
            })
        }
    })
});

//用户注册
router.post('/register', function (req, res, next) {
    if (!req.body.username) {
        res.json({
            status: 1,
            message: "用户名为空"
        })
    }
    if (!req.body.password) {
        res.json({
            status: 1,
            message: "密码为空"
        })
    }
    if (!req.body.userMail) {
        res.json({
            status: 1,
            message: "用户邮箱为空"
        })
    }
    if (!req.body.userPhone) {
        res.json({
            status: 1,
            message: "用户手机为空"
        })
    }
    user.findByUsername(req.body.username, function (err, userSave) {
        if (userSave.length != 0) {
            // res.json(userSave)
            res.json({
                status: 1,
                message: "用户已注册"
            })
        } else {
            var registerUser = new user({
                username: req.body.username,
                password: req.body.password,
                userMail: req.body.userMail,
                userPhone: req.body.userPhone,
                userAdmin: 0,

            })
            registerUser.save(function () {
                res.json({
                    status: 0,
                    message: "注册成功"
                    
                })
            })
        }
    })

});
//用户找回密码
router.post('/findPassword', function (req, res, next) {

    if (req.body.repassword) {
        {
            //验证mail和phone
            user.findUserPassword(req.body.username, req.body.userMail, req.body.userPhone, function (err, userFound) {
                if (userFound.length != 0) {
                    user.update({
                        _id: userFound[0]._id
                    }, {
                        password: req.body.repassword
                    }, function (err, userUpdate) {
                        res.json({
                            status: 0,
                            message: '更改成功',
                            data: userUpdate
                        })
                    })
                } else {
                    res.json({
                        status: 1,
                        message: "信息错误"
                    })
                }
            })
        }
    } else {
        if (!req.body.username) {
            res.json({
                status: 1,
                message: "用户名称为空"
            })
        }
        if (!req.body.userMail) {
            res.json({
                status: 1,
                message: "用户邮箱为空"
            })
        }
        if (!req.body.userPhone) {
            res.json({
                status: 1,
                message: "用户手机为空"
            })
        }
        user.findUserPassword(req.body.username, req.body.userMail, req.body.userPhone, function (err, userFound) {
            if (userFound.length != 0) {
                res.json({
                    status: 0,
                    message: "验证成功，请修改密码",
                    data: {
                        username: req.body.username,
                        userMail: req.body.userMail,
                        userPhone: req.body.userPhone
                    }
                })
            } else {
                res.json({
                    status: 1,
                    message: "信息错误"
                })
            }
        })
    }
});


//管理员登录
router.post('/adminLogin', function (req, res, next) {
    user.findAdmin(req.body.username, req.body.userAdmin, req.body.password, function (err, userAdmin) {
        if (userAdmin != 0) {

            res.json({
                // status: 0,
                data: {
                    user: userAdmin
                },
                message: "管理登录成功",
                type: "success",
                status: 0
            })
            // }
        } else {
            res.json({
                status: 1,
                message: "你没有权限",
                type: "error"
            })
        }
    })
});
router.post('/del', function (req, res, next) {
    user.remove({
        username: req.body.username
    }, function (err, doc) {
        {
            res.json({

            });
        }
    });

});
router.post('/userUpdate', function (req, res, next) {
    // let info = req.body.sendDate;
    user.findByUsername(
        req.body.userName,
        function () {
            var updateUser = new user({
                username: req.body.userName,
                password: req.body.userPassword,
                userMail: req.body.userMail,
                userPhone: req.body.userPhone,
                userAdmin: req.body.userAdmin,
            })
            updateUser.save(function () {
                res.json({
                    status: 0,
                    message: "更新成功",
                })
                // console.log(req.body.userName);
            })
        })
})
module.exports = router;
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/userList');
mongoose.connection.on('connected', function () {
    console.log('连接成功');
});
mongoose.connection.on('error', function () {
    console.log('连接失败');
});
mongoose.connection.on('disconnected', function () {
    console.log('连接断开');
});